package cn.gzmli.dtt.common.controll;


import cn.gzmli.dtt.common.config.cache.Config;
import cn.gzmli.dtt.common.entity.Message;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.entity.User;
import cn.gzmli.dtt.common.service.MessageService;
import cn.gzmli.dtt.common.utils.Constant;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/messageMgr")
@Ignore
public class MessageMgrControll {

	@Autowired
	private MessageService messageService;

//	@RequestMapping(value = "/listMessage")
//	@ResponseBody
//	public Map<String, Object> listMessage(
//            HttpServletRequest request,
//            HttpServletResponse response,
//            String title, String messageType, Integer status,
//            String sourceID, String sendBeginTime, String sendEndTime,
//            PageInfo pageInfo
//	)throws Exception {
//
//		Map<String, Object> result = new HashMap<String, Object>();
//		User user= Config.getUser(request);
//		List<Map<String,Object>> messages=messageService.queryMsg(user.getId(), title, messageType, status, sourceID, sendBeginTime, sendEndTime, pageInfo);
//		result.put("messages",messages);
//		result.put("pageInfo",pageInfo);
//		return result;
//	}

	@RequestMapping(value = "/listMessage")
	@ResponseBody
	public Result listMessage(
			HttpServletRequest request,
			HttpServletResponse response,
			String title, String messageType, Integer status,
			String sourceID, String sendBeginTime, String sendEndTime,
			PageInfo pageInfo
	)throws Exception {
		User user= Config.getUser(request);
		List<Map<String,Object>> messages=messageService.queryMsg(user.getId(), title, messageType, status, sourceID, sendBeginTime, sendEndTime, pageInfo);
		return new Result(Constant.SUCCESS,"获取异常事件成功！",messages,pageInfo);
	}

	/**
	 * @param request
	 * @param response
	 * @param msgID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/markReadMsg")
	@ResponseBody
	public Map<String, Object> markReadMsg(
			HttpServletRequest request,
			HttpServletResponse response,
			Integer msgID,
			PageInfo pageInfo
	)throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		User user= Config.getUser(request);
		Config.removeMsgCache(user.getId().toString(),msgID);
		Session webSocket=(Session)request.getSession().getAttribute("msgSocket");
		if(webSocket!=null&&webSocket.isOpen())
		Config.getUnReadMsgCount(request.getSession(),webSocket);
		result.put("state",1);
		result.put("msg","读取消息成功");
		return result;
	}
	
	@RequestMapping(value = "/getMsgsStatus")
	@ResponseBody
	public Map<String, Object> getMsgsStatus(
			HttpServletRequest request,
			String tIDS
	) throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		User user = Config.getUser(request);
		List<Map<String, Object>> messages = messageService.selectMsgsStatus(tIDS);
		result.put("status", 1);
		return result;
	}

	@RequestMapping(value = "/chageMsgsStatus")
	@ResponseBody
	public Map<String, Object> chageMsgsStatus(
			HttpServletRequest request,
			String tIDS
	) throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		User user = Config.getUser(request);
		messageService.updateMsgsStatus(tIDS);
		result.put("status", 1);
		return result;
	}

	/**
	 * 业务服务消息推送调用
	 * @author wondeful 20191024
	 * @param request
	 * @param response
	 * @param userId
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pushMessages")
	@ResponseBody
	public Result pushMessages(
			HttpServletRequest request,
			HttpServletResponse response,
			Integer userId,
			Message msg
	)throws Exception {
        Config.pushMsgToCache(userId,msg);
		return new Result(Constant.SUCCESS,"获取异常事件成功！",null);
	}

}
